<template>

  <div class="app-container">

    <h2 style="text-align: center;">新增通知</h2>

    <el-form label-width="120px">
      <el-form-item label="通知标题">
        <el-input v-model="courseInfo.title" placeholder=" 示例：各单位注意"/>
      </el-form-item>

      <!-- 所属分类 TODO -->
      <el-form-item label="通知分类">
        <el-select
          v-model="courseInfo.subjectParentId"
          placeholder="一级分类"
          @change="subjectLevelOneChanged">
          <el-option
            v-for="subject in subjectOneList"
            :key="subject.id"
            :label="subject.title"
            :value="subject.id"/>
        </el-select>
        <!-- 二级分类 -->
        <el-select v-model="courseInfo.subjectId" placeholder="二级分类">
          <el-option
            v-for="subject in subjectTwoList"
            :key="subject.id"
            :label="subject.title"
            :value="subject.id"/>
        </el-select>
      </el-form-item>

      <!-- 课程讲师 TODO -->
      <!-- 课程讲师 -->
      <el-form-item label="通知作者">
        <el-select
          v-model="courseInfo.teacherId"
          placeholder="请选择">

          <el-option
            v-for="teacher in teacherList"
            :key="teacher.id"
            :label="teacher.name"
            :value="teacher.id"/>

        </el-select>
      </el-form-item>

      <!-- 简介-->
      <el-form-item label="通知内容">
        <tinymce :height="300" v-model="courseInfo.description"/>
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import Industry from '@/api/tech/industry'
import Tinymce from '@/components/Tinymce' // 引入富文本组件
export default {
  // 声明组件
  components: { Tinymce },
  data() {
    return {
      saveBtnDisabled: false,
      courseInfo: {
        title: '',
        subjectId: '', // 二级分类id
        subjectParentId: '', // 一级分类id
        teacherId: '',
        lessonNum: 0,
        description: '',
        cover: '/static/01.jpg',
        price: 0
      },
      BASE_API: process.env.BASE_API, // 接口API地址
      teacherList: [], // 封装所有的讲师
      subjectOneList: [], // 一级分类
      subjectTwoList: []// 二级分类
    }
  },
  created() {
    this.findAllPeople() // 查询所有发布者
    this.getOneSubject() // 查询所有一级分类
  },
  methods: {
    init() {
        if (this.$route.params && this.$route.params.id) {
            const id = this.$route.params.id
            this.getInfo(id)
        } else { // 路径没有id值，做添加
            // 清空表单
            this.courseInfo = {}
        }
    },
    // 点击某个一级分类，触发change，显示对应二级分类
    subjectLevelOneChanged(value) {
      // value就是一级分类id值
      // 遍历所有的分类，包含一级和二级
      for (var i = 0; i < this.subjectOneList.length; i++) {
        // 每个一级分类
        var oneSubject = this.subjectOneList[i]
        // 判断：所有一级分类id 和 点击一级分类id是否一样
        if (value === oneSubject.id) {
          // 从一级分类获取里面所有的二级分类
          this.subjectTwoList = oneSubject.children
          // 把二级分类id值清空
          this.courseInfo.subjectId = ''
        }
      }
    },
    // 查询所有的一级分类
    getOneSubject() {
      Industry.getSubjectList()
        .then(response => {
          this.subjectOneList = response.data.list
        })
    },
    // 查询所有
    findAllPeople() {
      Industry.findAllPeople()
        .then(response => {
          this.teacherList = response.data.items
        })
    },
    // 通过ID查询
    getInfo(id) {
        Industry.getById(id)
            .then(response => {
            this.courseInfo = response.data.items
        })
    },

    saveOrUpdate() {
      Industry.add(this.courseInfo)
        .then(response => {
          // 提示
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          // 跳转到列表页
          this.$router.push({ path: '/policyLetters/notices/list'})
        })
    }
  }
}
</script>
<style scoped>
  .tinymce-container {
    line-height: 29px;
  }
</style>
